:: filepath: e:\qiping\QP_server\build.bat
@echo off

:: 设置输出编码为 UTF-8
chcp 65001 >nul

:: 获取构建时间
for /f "tokens=2 delims==" %%i in ('wmic os get localdatetime /value ^| findstr "="') do set BUILT=%%i
set BUILT=%BUILT:~0,4%-%BUILT:~4,2%-%BUILT:~6,2% %BUILT:~8,2%:%BUILT:~10,2%:%BUILT:~12,2%

:: 检查是否提供版本号参数
if "%~1"=="" (
    echo Usage: %~nx0 ^<version^>
    exit /b 1
)

set VERSION=%~1

:: 获取最后一次提交的 Git Commit
for /f %%i in ('git rev-parse HEAD') do set GITCOMMIT=%%i

:: 拼接 ldflags 参数
set LDFLAGS=-X 'gitee.com/night-tc/gobige/global.Built=%BUILT%'
set LDFLAGS=%LDFLAGS% -X 'gitee.com/night-tc/gobige/global.Version=%VERSION%'
set LDFLAGS=%LDFLAGS% -X 'gitee.com/night-tc/gobige/global.Gitcommit=%GITCOMMIT%'

:: 输出 LDFLAGS 内容
echo LDFLAGS: %LDFLAGS%

:: 构建命令
go build -v -ldflags "%LDFLAGS%" -o ..\bin .\server\...

if errorlevel 1 (
    echo 构建失败！
    exit /b 1
)

echo 构建成功！
